package me.ezitku.base.ScreenRecord

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.media.projection.MediaProjection
import android.media.projection.MediaProjectionManager
import android.os.Build
import androidx.fragment.app.Fragment
import me.ezitku.base.extensions.currentSdk
import me.ezitku.base.extensions.versionCode

var mediaProjection: MediaProjection? = null

fun Context.createMediaProjection(resultCode: Int, resultData: Intent): MediaProjection? {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        if (mediaProjection == null)
            mediaProjection = (getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager).getMediaProjection(resultCode, resultData)
        return mediaProjection
    }
    return null
}


/**
 * ئېكراننى سىنغا ئېلىشى ئۈچۈن ئاندىرويىد نەشىرى چوقۇم 5.0 ياكى ئۇنىڭدىن يۇقىرى بولىىش كېرەك
 * 2. چوقۇم ئاۋال ئېكراننى سىنغا ئېلىشى ئىلتىماسى يوللاپ ھوقۇق ئېلىشىمىز كېرەك
 * 3.رەسىمگە تۇتۇش ئۈچۈنمۇ مۇشۇ ھوقۇقنى ئىلتىماس قىلىشىمىز كېرەك
 */
fun Activity.requestScreenRecorder(request_code: Int) {
    if (currentSdk >= versionCode.LOLLIPOP) {
        val mediaProjectionManager = getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
        val permissionIntent = mediaProjectionManager.createScreenCaptureIntent()
        startActivityForResult(permissionIntent, request_code)
    }
}

fun Fragment.requestScreenRecorder(request_code: Int = 999) {
    activity!!.requestScreenRecorder(request_code)
}